package com.example.demo.controller;

import com.example.demo.config.NettyClientSington;
import com.example.demo.mapper.DeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Component
public class ScheduledTasks {

    @Autowired
    private DeviceMapper deviceMapper;

    // 定时任务：每5秒执行一次

    @Async
    @Scheduled(fixedRate = 5000)
    public void pingDevice() throws InterruptedException {

        //发送 {"device":"YIHP","target":"all","transtype":"0001","sn":" G11070012695995260120221117003N105 "}
        String ping = "{\"device\":\"YIHP\",\"target\":\"all\",\"transtype\":\"0001\",\"sn\":\" G11070012695995260120221117003N105 \"} ";
        NettyClientSington.getNettyClient().sendMessage(ping, "All");


    }


    // 使用cron表达式定义定时任务
    @Scheduled(cron = "0 0 12 * * ?")
    public void fixedTimeTask() {
        System.out.println("执行固定时间任务：" + System.currentTimeMillis());
    }

}